package indi.h.filefilter.filters;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;

import indi.h.filefilter.anaysis.Anaysis;

public class DocImageFilter extends BasicFilter {
	private final static Logger l = Logger.getLogger(DocImageFilter.class);

	public DocImageFilter(Enum title) {
		// TODO Auto-generated constructor stub
		super(title);
	}

	@Override
	public boolean filter(Anaysis.FileMessage af) {
		// TODO Auto-generated method stub
		super.filter(af);
		if (isFilter()) {
			l.debug("start doc image filter");
			try (InputStream in = new FileInputStream(af.getMessage()); HWPFDocument d = new HWPFDocument(in)) {
				PicturesTable p = d.getPicturesTable();
				for (int i = 0; i < d.characterLength(); i++) {
					Range range = new Range(i, i + 1, d);
					CharacterRun cr = range.getCharacterRun(0);
					if (p.hasPicture(cr)) {
						return false;
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				l.error("Exception:" + e.getMessage());
				return false;
			}
		}
		return true;
	}
}
